<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      :page.sync="page"
      :search.sync="query"
      :permission="permissionList"
      :before-open="beforeOpen"
      :table-loading="loading"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view" @click="viewDetail(row)">包装详情</el-button>
      </template>
    </avue-crud>
    <el-dialog
      v-dragDirectives
      append-to-body
      :close-on-click-modal="false"
      title="包装详情"
      :visible.sync="showDialog"
      v-if="showDialog"
      width="60%"
      top="5vh"
      @close="closeShowDialog"
    >
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>基本信息</h3>
      <avue-form ref="basicInfo" v-model="basicInfo" :option="basicInfoOption"></avue-form>
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>物料报废信息</h3>
      <avue-crud
        :option="scrapDetailOption"
        @current-change="currentDetailChange"
        @size-change="sizeDetailChange"
        :page.sync="scrapDetailPage"
        :table-loading="scrapDetailLoading"
        :data="crapDetailData"
        ref="scrapDetailCrud"
      >
        <template slot-scope="{ row }" slot="menu">
          <el-button type="text" size="small" icon="el-icon-view" @click="viewChildDetail(row)" :disabled="!row.isHasSubPack">子级包装</el-button>
        </template>
      </avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <el-dialog
      v-dragDirectives
      append-to-body
      :close-on-click-modal="false"
      title="子级包装详情"
      :visible.sync="showChildDialog"
      v-if="showChildDialog"
      width="60%"
      top="5vh"
      @close="closeShowChildDialog"
    >
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>基本信息</h3>
      <avue-form ref="childBasicInfo" v-model="childBasicInfo" :option="childBasicInfoOption"></avue-form>
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>子级包装信息</h3>
      <avue-crud :option="scrapDetailOption" :table-loading="scrapDetailChildLoading" :data="crapDetailChildData" ref="scrapDetailChildCrud">
        <template slot-scope="{ row }" slot="menu">
          <el-button type="text" size="small" icon="el-icon-view" @click="viewChildDetail(row)">子级包装</el-button>
        </template>
      </avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeShowChildDialog"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, getDetail, getChildDetail } from '@/api/warehouse/rawMaterialsScrapRecordDetail';
import { mapGetters } from 'vuex';

export default {
  components: {},
  data() {
    return {
      form: {},
      query: {},
      exportParams: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      option: {
        height: 'calc(100vh - 366px)',
        columnBtn: false,
        calcHeight: 30,
        tip: false,
        refreshBtn: true, //刷新按钮是否显示
        searchBtn: true, // 显示查询的按钮
        border: true,
        index: true,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        viewBtn: false,
        width: '100',
        menu: true,
        searchLabelWidth: 100,
        searchMenuPosition: 'right',
        searchMenuSpan: 24,
        searchSpan: 6,
        searchShow: true,
        indexLabel: '序号',
        column: [
          {
            label: '仓库名称',
            prop: 'warehouseName',
            search: true,
            searchOrder: 1
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '物料名称',
            prop: 'partName',
            search: true,
            searchOrder: 4
          },
          {
            label: '物料规格',
            prop: 'partSpec',
            search: true,
            searchOrder: 3
          },

          {
            label: '报废数量',
            prop: 'quantity'
          }
        ]
      },
      showDialog: false,
      basicInfoOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '仓库名称',
            prop: 'warehouseName',
            disabled: true
          },
          {
            label: '物料编码',
            prop: 'partNo',
            disabled: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            disabled: true
          },
          {
            label: '报废数量',
            prop: 'quantity',
            disabled: true
          }
        ]
      },
      basicInfo: {},
      scrapDetailOption: {
        height: '350',
        columnBtn: false,
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        viewBtn: false,
        menu: true,
        menuWidth: 100,
        indexLabel: '序号',
        column: [
          {
            label: '包装条码',
            prop: 'packingSn'
          },
          {
            label: '报废数量',
            prop: 'scrapQuantity'
          },
          {
            label: '品牌',
            prop: 'partBrand'
          },
          {
            label: 'D/C',
            prop: 'dateCode'
          },
          {
            label: '内部批次',
            prop: 'lotNo'
          },
          {
            label: '报废人',
            prop: 'scrapByName'
          },
          {
            label: '报废时间',
            prop: 'scrapTime'
          }
        ]
      },
      crapDetailData: [],
      scrapDetailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      scrapDetailLoading: false,
      showChildDialog: false,
      childBasicInfoOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '仓库名称',
            prop: 'warehouseName',
            disabled: true
          },
          {
            label: '物料编码',
            prop: 'partNo',
            disabled: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            disabled: true
          },
          {
            label: '报废数量',
            prop: 'quantity',
            disabled: true
          },
          {
            label: '包装条码',
            prop: 'packingSn',
            disabled: true
          }
        ]
      },
      childBasicInfo: {},
      scrapDetailChildLoading: false,
      crapDetailChildData: []
    };
  },
  filters: {},
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {};
    }
  },
  methods: {
    beforeOpen(done, type) {
      done();
    },
    searchReset(params, done) {
      this.query = {};
      this.page = {
        pageSize: 20,
        currentPage: 1,
        total: 0
      };
      this.onLoad(this.page, this.query);
      done();
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.data = data.records;
          this.page.total = data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    viewDetail(row) {
      this.showDialog = true;
      this.basicInfo = row;
      this.scrapDetailOption.menu = true;
      this.getDetailInfo();
    },
    getDetailInfo() {
      this.scrapDetailLoading = true;
      let params = {
        warehouseId: this.basicInfo.warehouseId,
        partMasterId: this.basicInfo.partMasterId
      };
      getDetail(this.scrapDetailPage.currentPage, this.scrapDetailPage.pageSize, params)
        .then((res) => {
          const data = res.data.data;
          this.crapDetailData = data.records;
          this.scrapDetailPage.total = data.total;
        })
        .finally(() => {
          this.scrapDetailLoading = false;
        });
    },
    currentDetailChange(currentPage) {
      this.scrapDetailPage.currentPage = currentPage;
      this.getDetailInfo();
    },
    sizeDetailChange(pageSize) {
      this.scrapDetailPage.pageSize = pageSize;
      this.getDetailInfo();
    },
    closeShowDialog() {
      this.basicInfo = {};
      this.$refs.basicInfo.resetFields();
      this.crapDetailData = [];
      this.scrapDetailPage = {
        pageSize: 20,
        currentPage: 1,
        total: 0
      };
    },
    viewChildDetail(row) {
      this.showChildDialog = true;
      this.scrapDetailOption.menu = false;
      this.childBasicInfo = row;
      this.childBasicInfo.quantity = row.scrapQuantity;
      this.scrapDetailChildLoading = true;
      getChildDetail(row.id).then((res) => {
        this.crapDetailChildData = this._get(res, 'data.data', []);
        this.scrapDetailChildLoading = false;
      });
    },
    closeShowChildDialog() {
      this.showChildDialog = false;
      this.childBasicInfo = {};
      this.$refs.childBasicInfo.resetFields();
      this.crapDetailChildData = [];
      this.scrapDetailOption.menu = true;
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },

  watch: {}
};
</script>
<style lang="scss" scoped>
.custom-title {
  font-size: 16px;
  font-weight: bold;
  .mr10 {
    margin-right: 10px;
  }
}
::v-deep .el-card__body {
  .avue-crud__menu {
    display: none;
  }
}
</style>
